package com.techstar.ats.codec.live.decoder;

import io.netty.buffer.ByteBuf;
import com.techstar.ats.codec.DateTimeCodec;
import com.techstar.ats.codec.Decoder;
import com.techstar.ats.codec.StringCodec;
import com.techstar.ats.dxp.live.TrainIndication;
import com.techstar.ats.type.DeviceType;

public enum TrainIndicationDecoder implements Decoder<TrainIndication> {
	INSTANCE;

	public TrainIndication create() {
		return new TrainIndication();
	}

	public void decode(ByteBuf buf, TrainIndication value) {
		value.rtu = buf.readUnsignedShort();
		value.mmccWin = buf.readUnsignedShort();
		value.mmccWinOffset = buf.readUnsignedByte();
		value.devType = DeviceType.valueOf(buf.readUnsignedShort());
		value.devName = (new StringCodec(24)).get(buf);
		value.trainIdx = (new StringCodec(10)).get(buf);
		value.group = (new StringCodec(9)).get(buf);
		value.train = (new StringCodec(9)).get(buf);
		value.global = (new StringCodec(12)).get(buf);
		value.dest = (new StringCodec(4)).get(buf);
		value.rollingstock = buf.readUnsignedByte();
		value.driver = (new StringCodec(13)).get(buf);
		if(value.hasRoute) {
			value.route = buf.readUnsignedShort();
		}

		value.optTime = buf.readInt();
		value.mode = buf.readInt();
		value.arriveTime = DateTimeCodec.INSTANCE.get(buf);
		value.departTime = DateTimeCodec.INSTANCE.get(buf);
		value.rate = buf.readInt();
		value.speed = buf.readUnsignedByte();
		buf.skipBytes(2);
	}
}
